// mircoApp/horizon/deviceList/deviceList.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    deviceType: 0,
    show: false,
    popupKey: "",
    machine_type: [],
    storeData: [],
    member_time_count: 0,
    can_buy: 0,
    is_owner: 1,
    current_page: 0,
    last_page: 0,
    page: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.customer();
  },
  toRecord(e) {
    let _url = '/mircoApp/shop_base/reward_template/index';
    let {
      machine_id,
      member_time_id
    } = e.currentTarget.dataset
    let extparams = {}
    if(machine_id){
      extparams.machine_id  = machine_id;
      _url += `?extparams=${JSON.stringify(extparams)}&api=plugin.horizon.frontend.machine.machineTimesLog`
    }
    if(member_time_id){
      extparams.member_time_id  = member_time_id;
      _url += `?extparams=${JSON.stringify(extparams)}&api=plugin.horizon.frontend.customer.countLog`
    }
    wx.navigateTo({
      url: _url,
    })
  },
  tapRecord() {
    // 使用中设备
    wx.navigateTo({
      url: '/mircoApp/horizon/serviceDevice/serviceDevice',
    })
  },
  changeTab(e) {
    console.log(e)
    this.setData({
      active: e.detail.index
    })
    if (e.detail.index == 1) {
      this.get_machineType();
    } else {
      this.customer();
    }
  },
  get_machineType() {
    let urlStr = app.getNetAddresss("plugin.horizon.frontend.machine.machineType");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            machine_type: res.data
          })
          console.log(this.data.machine_type, 'dddddddddd')
          if (this.data.machine_type.length > 0) {
            this.setData({
              deviceType: this.data.machine_type[0].id
            })
            this.getData();
          }
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  customer() {
    let urlStr = app.getNetAddresss("plugin.horizon.frontend.customer.countList");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res)
          this.setData({
            storeData: res.data
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.horizon.frontend.machine.machineList");
    app._getNetWork({
      url: urlStr,
      data: {
        machine_type: this.data.deviceType,
        page: this.data.page
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            can_buy: res.data.can_buy,
            is_owner: res.data.is_owner,
            member_time_count: res.data.member_time_count,
            list: res.data.list.data,
            current_page: res.data.list.current_page,
            member_time_id:res.data.member_time_id,
            last_page: res.data.list.last_page
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  toRaise() {
    wx.navigateTo({
      url: '/mircoApp/horizon/raisePrice/raisePrice',
    })
  },
  tapMoreShow(e){
    let {ind} = e.currentTarget.dataset;
    this.setData({
      ['storeData['+ind+'].moreShow'] : true
    })
  },
  closeMoreShow(e){
    let {ind} = e.currentTarget.dataset;
    this.setData({
      ['storeData['+ind+'].moreShow'] : false
    })
  },
  scanCode() {
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        console.log(res)
        let {
          result
        } = res;
        let str = result.substr(result.lastIndexOf('/') + 1);
        console.log(str)
        wx.showModal({
          title: '是否绑定当前设备?',
          content: str,
          complete: (res) => {
            if (res.confirm) {
              console.log(res.result)
              wx.navigateTo({
                url: '/mircoApp/horizon/horizonDevice/horizonDevice?mac=' + str,
              })
            }
          }
        })
      }
    })
  },
  closeShow(e) {
    console.log(e);
    if (e.detail) {
      //点击确定就是有更改重新请求页面接口
      this.setData({
        active: 1,
        page: 1
      })
      this.getData();
    }
    this.setData({
      show: false
    })
  },
  tapChild(e) {
    let {
      ind
    } = e.currentTarget.dataset;
    this.setData({
      deviceType: ind
    })
    this.getData();
  },
  showModel(e) {
    let {
      key,
      item
    } = e.currentTarget.dataset;
    console.log(key)
    this.setData({
      popupKey: key,
      show: true,
      childPopup: item
    })
  },
  ToBuySearch() {
    wx.navigateTo({
      url: `/packageB/member/category/search_v2/search_v2?machine_type=${this.data.deviceType}&tag=horizon`,
    })
  }
})